package com.lpf.interpreter;

// 上下文类 - 表达式解析器
class ExpressionParser {
    public static Expression parse(String expression) {
        String[] tokens = expression.split(" ");
        Expression result = new NumberExpression(Integer.parseInt(tokens[0]));

        for (int i = 1; i < tokens.length; i += 2) {
            String operator = tokens[i];
            int number = Integer.parseInt(tokens[i + 1]);

            if (operator.equals("+")) {
                result = new AddExpression(result, new NumberExpression(number));
            } else if (operator.equals("-")) {
                result = new SubtractExpression(result, new NumberExpression(number));
            }
        }

        return result;
    }
}
